<template>
    <div style="height: 100vh; overflow: hidden;position: relative">
        <el-card class="cover" v-if="loginAdmin.id">
<!--            :accuracy滑动验证的误差范围，默认值 5-->
            <slide-verify :l="42"
                          :r="10"
                          :w="310"
                          :h="155"
                          :accuracy="5"
                          slider-text="向右滑动"
                          @success="onSuccess"
                          @fail="onFail"
                          @refresh="onRefresh"
            ></slide-verify>
        </el-card>


        <div style="width: 500px;height: 400px;background-color: white;border-radius: 10px;margin: 150px auto;padding: 50px">
            <div style="margin: 30px; text-align: center;font-size: 30px;font-weight: bold;color: dodgerblue">登 录</div>
            <el-form :model="admin" ref="loginFrom" :rules="rules">
                <el-form-item prop="username">
                    <el-input placeholder="请输入用户名" prefix-icon="el-icon-user" size="medium"
                              v-model="admin.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" show-password prefix-icon="el-icon-lock" size="medium"
                              v-model="admin.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 100%" size="medium" type="primary" @click="login">登录</el-button>
                </el-form-item>
            </el-form>

        </div>

    </div>
</template>

<script>
    import request from "@/utils/request";
    import Cookies from "js-cookie"

    export default {
        name: 'Login',
        data() {
            return {
                loginAdmin: {},
                admin: {},
                rules: {
                    //用户名不能为空
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        //最小三个字符 最大10个字符
                        // {min: 3, max: 10, message: '长度在3-10个字符之间', trigger: 'blur'}
                    ],

                    //密码
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},

                    ]
                }

            }
        },
        methods: {
            login() {
                this.$refs['loginFrom'].validate((valid) => {
                    if (valid) {
                        request.post("/admin/login", this.admin).then(res => {
                            if (res.code === '200') {
                                //登录成功 为loginAdmin.id 赋值，出现滑块
                                this.loginAdmin = res.data //滑块组件出现了

                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                })
            },
            onSuccess() {//滑块验证通过后触发
                //保存Cookies
                Cookies.set('admin', JSON.stringify(this.loginAdmin))
                this.$router.push('/')
                this.$notify.success("登录成功")
            },
            onFail() {
                console.log('onFail')
            },
            onRefresh() {
                console.log('refresh')
                //this.$notify.warning('手贱')
            }

        }
    }
</script>
<style>
    .cover {
        width: fit-content;
        background-color: white;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 1000;
    }
</style>